﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    /// <summary>
    /// 部门
    /// </summary>
    public class T_DeparetmentDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加部门
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_Department(T_Deparetment en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///同一级别下不能有一样名字的部门
                if (conn.T_Deparetment.Select(c => c).Where(c => c.Dep_PID == en.Dep_PID && c.Dep_Name == en.Dep_Name).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_Deparetment.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_Deparetment(T_Deparetment en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///同一个PID里的不能有名字重复的部门名字,并且ID号不相同
                if (conn.T_Deparetment.Select(c => c).Where(c => c.Dep_ID != en.Dep_ID && c.Dep_PID == en.Dep_PID && c.Dep_Name == en.Dep_Name).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_Deparetment.Select(c => c).Where(c => c.Dep_ID == en.Dep_ID);
                    foreach (var n in query)
                    {
                        if (!SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "Dep_ID"))
                            return _ReturnValue.Err;
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 修改部门名称
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static _ReturnValue Modify_Deparetment(int id, string name, int status)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                ///同一个PID里的不能有名字重复的部门名字,并且ID号不相同
                if (conn.T_Deparetment.Select(c => c).Where(c => c.Dep_ID != id && c.Dep_Name == name).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    var query = conn.T_Deparetment.Select(c => c).Where(c => c.Dep_ID == id);
                    foreach (var n in query)
                    {
                        n.Dep_Name = name;
                        n.Dep_Status = status;
                    }
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 获取部门
        /// </summary>
        /// <param name="pid">是否是主部门节点</param>
        /// <param name="status">是否启用</param>
        /// <param name="start">分页的起始行，-1 代表不分页</param>
        /// <param name="row">每页显示数， -1 代表不分页</param>
        /// <param name="count">总数</param>
        /// <returns></returns>
        public static List<T_Deparetment> Get_Department(out int count, int status = 1, int pid = -1, int start=-1, int row=-1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_Deparetment.Select(c => c);
                if (pid > -1)
                    query = query.Where(c => c.Dep_PID == pid);
                if (status > -1)
                    query = query.Where(c => c.Dep_Status == status);
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.Dep_PID).ThenBy(c => c.Dep_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.OrderBy(c => c.Dep_PID).ThenBy(c => c.Dep_ID).ToList();
                    count = list.Count();
                    return list;
                }
            }
            catch(Exception ex) { return null; }
        }

        /// <summary>
        /// 根据ID获取一个部门信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T_Deparetment Get_Department(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_Deparetment.Select(c => c).Where(c => c.Dep_ID == id).Single();
            }
            catch { return null; }
        }

        public static _ReturnValue Set_DepartmentAuthority(int id, List<T_DepartmentAuthority> list)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_Deparetment.Select(c => c).Where(c => c.Dep_ID == id);
                foreach(var n in query)
                {
                    string show = "";
                    string view = "";
                    string edit = "";
                    string print = "";
                    string down = "";
                    string app = "";
                    foreach(var p in list)
                    {
                        if (p.DepAut_Show == 1)
                            show += p.DepAut_DtoID + ",";
                        if (p.DepAut_View == 1)
                            view += p.DepAut_DtoID + ",";
                        if (p.DepAut_Print == 1)
                            print += p.DepAut_DtoID + ",";
                        if (p.DepAut_Down == 1)
                            down += p.DepAut_DtoID + ",";
                        if (p.DepAut_Edit == 1)
                            edit += p.DepAut_DtoID + ",";
                        if (p.DepAut_Approval == 1)
                            app += p.DepAut_DtoID + ",";
                    }

                    n.Dep_Show = show;
                    n.Dep_View = view;
                    n.Dep_Print = print;
                    n.Dep_Down = down;
                    n.Dep_Edit = edit;
                    n.Dep_Approval = app;
                }
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }
    }
}
